using UnityEngine;

public class PlayerGroundedState : PlayerState
{
    public PlayerGroundedState(Player player, string animBoolName) : base(player, animBoolName) { }

    public override void Enter()
    {
        base.Enter();
        player.SetVelocity(0, rb.velocity.y);
    }

    public override void Update()
    {
        base.Update();
        if (Input.GetKeyDown(KeyCode.R))
        {
            if (!player.SkillManager.Blackhole.BlackholeUnlocked) return;
            if (player.SkillManager.Blackhole.cooldownTimer > 0f)
            {
                player.Fx.CreatePopUp("Cooldown");
                return;
            }
            StateMachine.ChangeState(player.BlackholeState);
        }
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            if (!player.SkillManager.Sword.SwordUnlocked) return;
            if (!HasNoSword()) return;
            StateMachine.ChangeState(player.AimSwordState);
        }
        if (Input.GetKeyDown(KeyCode.Q))
        {
            if (!skillManager.Parry.ParryUnlocked) return;
            StateMachine.ChangeState(player.CounterAttackState);
        }
        if (Input.GetKey(KeyCode.Mouse0) || Input.GetKey(KeyCode.J))
        {
            StateMachine.ChangeState(player.PrimaryAttackState);
        }
        if (!player.IsGroundDetected())
        {
            StateMachine.ChangeState(player.AirState);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (!player.IsGroundDetected()) return;
            StateMachine.ChangeState(player.JumpState);
        }
    }

    private bool HasNoSword()
    {
        if (!player.Sword)
        {
            return true;
        }
        player.Sword.GetComponent<SwordSkill>().ReturnSword();
        return false;
    }
}